package vg.services.graph_view_manager.realization.graph_comparison.renderers;

import java.awt.Component;
import java.lang.reflect.InvocationTargetException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import vg.services.data_base_manager.data.skeleton.GraphTreeElement;

public class SmartGraphTreeComparisonRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 1L;
	//-------------------------------------------------------------------------
	private final Icon routineIcon;
	private final Icon vertexIcon;
	private final Icon tabIcon;
	private TreeCellRenderer delegate;
	/**
	 * Default constructor.
	 */
	public SmartGraphTreeComparisonRenderer() {
		this.routineIcon = new ImageIcon("data/resources/textures/workflow.png");
		this.vertexIcon = new ImageIcon("data/resources/textures/vertexBall.png");
		this.tabIcon = new ImageIcon("data/resources/textures/tab.png");
	}
	//-------------------------------------------------------------------------
	public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean selected, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
		if (SwingUtilities.isEventDispatchThread()) {
			delegate = new JTree().getCellRenderer();
		} else {
			try {
				SwingUtilities.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						delegate = new JTree().getCellRenderer();
					}
				});
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		Component returnValue = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		if (delegate instanceof JLabel) {
			JLabel renderer = (JLabel) delegate;
			if ((value != null) && (value instanceof GraphTreeElement)) {
				GraphTreeElement node = (GraphTreeElement)value;
				
				if((node.vg_getType() & GraphTreeElement.DEF_GRAPH_TYPE) > 0) {
					renderer.setIcon(routineIcon);
				} else if ((node.vg_getType() & GraphTreeElement.DEF_VERTEX_TYPE) > 0){
					renderer.setIcon(vertexIcon);
				} else if ((node.vg_getType() & GraphTreeElement.DEF_TAB_TYPE) > 0){
					renderer.setIcon(tabIcon);
				} else {
					renderer.setIcon(null);
				}
			}	
		}
		return returnValue;
	}
}	